package com.example.online_exam_system.dao;

import com.example.online_exam_system.dto.RegisterDto;
import com.example.online_exam_system.entity.Student;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * (Student)表数据库访问层
 *
 * @author lianglong@asiainfo.com
 * @since 2021-11-25 14:28:46
 */
public interface StudentDao {

    /**
     * 通过ID查询单条数据
     *
     * @param studentid 主键
     * @return 实例对象
     */
    Student queryById(Object studentid);

    /**
     * @description 通过用户名查找用户
     * @param inParam
     * @return map
     * @author lianglong
     */
    @Select("select * from student where username = #{inParam.username} and password = #{inParam.password}")
    List<Map<String, Object>> queryByuserName(@Param("inParam") Map<String,Object> inParam);

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    List<Student> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);


    /**
     * 通过实体作为筛选条件查询
     *
     * @param student 实例对象
     * @return 对象列表
     */
    List<Student> queryAll(Student student);

    /**
     * 新增数据
     *
     * @param student 实例对象
     * @return 影响行数
     */
    int insert(Student student);

    @Insert("INSERT INTO exam_system.student (\n" +
            "  username,\n" +
            "  password,\n" +
            "  regtime,\n" +
            "  serialnumber,\n" +
            "  mailnumber\n" +
            ")\n" +
            "VALUES\n" +
            "  (#{inParam.username}, #{inParam.password},#{inParam.regtime}, #{inParam.phone}, #{inParam.email})")
    int insert(@Param("inParam") RegisterDto registerDto);;

    /**
     * 批量新增数据（MyBatis原生foreach方法）
     *
     * @param entities List<Student> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(@Param("entities") List<Student> entities);

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param entities List<Student> 实例对象列表
     * @return 影响行数
     */
    int insertOrUpdateBatch(@Param("entities") List<Student> entities);

    /**
     * 修改数据
     *
     * @param student 实例对象
     * @return 影响行数
     */
    int update(Student student);

    /**
     * 通过主键删除数据
     *
     * @param studentid 主键
     * @return 影响行数
     */
    int deleteById(Object studentid);

    @Select("select studentid,username,realname,regtime,college,serialnumber,mailnumber from student where username = #{username}")
    Map<String, Object> getStudentInfo(@Param("username") String username);

    @Select("select password from student where username =#{inParam.username}")
    String getPassword(@Param("inParam") Map<String, Object> inParam);

    @Update("update student set username=#{inParam.username},password=#{inParam.password},serialnumber=#{inParam.serialnumber}," +
            "mailnumber=#{inParam.mailnumber}, college=#{inParam.college} where realname =#{inParam.realname} ")
    int updateStudentInfo(@Param("inParam") Map<String, Object> inParam);

    @Select("SELECT studentid,username,realname,regtime,serialnumber,mailnumber,college FROM student")
    List<Map<String, Object>> getStuAll();
}

